package com.lh.center.common.web;

import com.lh.center.common.util.LoggerPrintInterceptor;
import com.lh.center.common.util.SpringUtils;
import com.lh.center.common.web.advice.*;
import com.lh.center.common.web.config.BizLoggerPrintInterceptor;
import com.lh.center.common.web.handle.*;
import com.lh.center.common.web.session.SessionOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.util.HashSet;
import java.util.Set;

/**
 * 自动装配
 */
@Configuration
@Import({BeforeRequestHandle.class,
        BeforeResponseHandle.class,
        GlobalExceptionHandler.class,
        WebMvcConfig.class,
        JsonRequestHandle.class,
        SpringUtils.class,
        SessionOperation.class})
@ConfigurationProperties(prefix = "web")
public class CommonWebAutoConfiguration {

    /** 是否开启配置*/
    private Boolean enable = true;

    /** 是否按配置检查用户登陆状态*/
    private boolean enableAuth = false;

    /** 是否开启参数加解密*/
    private boolean enableSecret = false;

    /** 是否开启响应参数包装*/
    private boolean enablePackage = true;

    /** 是否开启返回异常堆栈信息*/
    private boolean enableErrorTrace = true;

    /** 日志打印是否要包含请求参数*/
    private boolean logWithRequest = true;

    /** 日志打印是否要包含响应参数*/
    private boolean logWithResponse = true;

    /** 日志打印是否要包含当前用户信息*/
    private boolean logWithUser = true;

    /** 指定打印的用户账号，调试时可针对固定账号开启，为空时认为不指定*/
    private String logByAssignAccount = "";

    /** token在header中的名字*/
    private String tokenName = "token";

    /** 默认用户id*/
    private Long defaultUserId = 1L;

    /** 不需要校验登陆的接口*/
    private Set<String> notAuthUri = new HashSet<>();

    /** 不需要被包装返回值的接口*/
    private Set<String> notPackageUri = new HashSet<>();

    /** 不需要进行加解密处理的接口*/
    private Set<String> notSecretUri = new HashSet<>();

    /** 开启请求和响应参数日志的接口*/
    private Set<String> logUri = new HashSet<>();

    @Bean
    @ConditionalOnMissingBean(BeforeRequestAdvice.class)
    public BeforeRequestAdvice beforeRequestAdvice(){
        return new DefaultBeforeRequestAdvice();
    }

    @Bean
    @ConditionalOnMissingBean(BeforeResponseAdvice.class)
    public BeforeResponseAdvice beforeResponseAdvice(){
        return new DefaultBeforeResponseAdvice();
    }

    @Bean
    @ConditionalOnMissingBean(DefaultGlobalExceptionAdvice.class)
    public GlobalExceptionAdvice globalExceptionAdvice(){
        return new DefaultGlobalExceptionAdvice();
    }

    @Bean
    @ConditionalOnMissingBean(JsonRequestAdvice.class)
    public JsonRequestAdvice jsonRequestAdvice(){
        return new DefaultJsonRequestAdvice();
    }

    @Bean
    @ConditionalOnMissingBean(LoggerPrintInterceptor.class)
    public LoggerPrintInterceptor loggerPrintInterceptor(){
        return new BizLoggerPrintInterceptor();
    }

    public Boolean getEnable() {
        return enable;
    }

    public void setEnable(Boolean enable) {
        this.enable = enable;
    }

    public String getTokenName() {
        return tokenName;
    }

    public void setTokenName(String tokenName) {
        this.tokenName = tokenName;
    }

    public Set<String> getNotAuthUri() {
        return notAuthUri;
    }

    public void setNotAuthUri(Set<String> notAuthUri) {
        this.notAuthUri = notAuthUri;
    }

    public Set<String> getNotPackageUri() {
        return notPackageUri;
    }

    public void setNotPackageUri(Set<String> notPackageUri) {
        this.notPackageUri = notPackageUri;
    }

    public Set<String> getNotSecretUri() {
        return notSecretUri;
    }

    public void setNotSecretUri(Set<String> notSecretUri) {
        this.notSecretUri = notSecretUri;
    }

    public Long getDefaultUserId() {
        return defaultUserId;
    }

    public void setDefaultUserId(Long defaultUserId) {
        this.defaultUserId = defaultUserId;
    }

    public boolean isEnableAuth() {
        return enableAuth;
    }

    public void setEnableAuth(boolean enableAuth) {
        this.enableAuth = enableAuth;
    }

    public boolean isEnableSecret() {
        return enableSecret;
    }

    public void setEnableSecret(boolean enableSecret) {
        this.enableSecret = enableSecret;
    }

    public boolean isEnablePackage() {
        return enablePackage;
    }

    public void setEnablePackage(boolean enablePackage) {
        this.enablePackage = enablePackage;
    }

    public Set<String> getLogUri() {
        return logUri;
    }

    public void setLogUri(Set<String> logUri) {
        this.logUri = logUri;
    }

    public boolean isEnableErrorTrace() {
        return enableErrorTrace;
    }

    public void setEnableErrorTrace(boolean enableErrorTrace) {
        this.enableErrorTrace = enableErrorTrace;
    }

    public boolean isLogWithRequest() {
        return logWithRequest;
    }

    public void setLogWithRequest(boolean logWithRequest) {
        this.logWithRequest = logWithRequest;
    }

    public boolean isLogWithResponse() {
        return logWithResponse;
    }

    public void setLogWithResponse(boolean logWithResponse) {
        this.logWithResponse = logWithResponse;
    }

    public boolean isLogWithUser() {
        return logWithUser;
    }

    public void setLogWithUser(boolean logWithUser) {
        this.logWithUser = logWithUser;
    }

    public String getLogByAssignAccount() {
        return logByAssignAccount;
    }

    public void setLogByAssignAccount(String logByAssignAccount) {
        this.logByAssignAccount = logByAssignAccount;
    }
}
